package com.dmzc.stage1.chapter6;

import java.util.concurrent.TimeUnit;

public class ThreadGroupEnumerateThreads {
    public static void main(String[] args)
            throws InterruptedException
    {
        ThreadGroup myGroup=new ThreadGroup("MyGroup");
        Thread thread=new Thread(myGroup,()->{
            while(true){
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"MyThread");
        thread.start();
        TimeUnit.MILLISECONDS.sleep(2);
        ThreadGroup mainGroup=Thread.currentThread().getThreadGroup();
        Thread[]list=new Thread[mainGroup.activeCount()];
        Thread[]list1=new Thread[mainGroup.activeCount()];
        int recurseSize=mainGroup.enumerate(list);
        System.out.println(recurseSize);
        for (int i =0;i<list.length;i++){
            Thread t=list[i];
            System.out.println(t.getName());
        }

        recurseSize=mainGroup.enumerate(list1,false);
        for (int i =0;i<list1.length;i++){
            Thread t=list1[i];
            if (t!=null){
                System.out.println(t.getName());
            }

        }
        System.out.println(recurseSize);
    }
}
